package com.cn.aam.checaiduo.ui.more;

import android.text.TextUtils;
import android.widget.EditText;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.cn.aam.checaiduo.AppData;
import com.cn.aam.checaiduo.Const;
import com.cn.aam.checaiduo.R;
import com.cn.aam.checaiduo.backend.RetrofitHome;
import com.cn.aam.checaiduo.backend.observer.OnRequestListener;
import com.cn.aam.checaiduo.backend.rx.RxSchedulers;
import com.cn.aam.checaiduo.base.BaseActivity;
import com.cn.aam.checaiduo.base.ToolBarManager;
import com.cn.aam.checaiduo.bean.base.BaseResponseB;
import com.cn.aam.checaiduo.ui.arouter.UIRouter;
import com.cn.aam.checaiduo.ui.arouter.section.RouterMore;
import com.cn.aam.checaiduo.utils.MyEditTextUtils;
import com.cn.aam.checaiduo.widget.WMToast;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = UIRouter.MORE.CHANGE_MOBILE_PWD, name = "修改登录密码")
public class ChangePwdActivity extends BaseActivity {

    @BindView(R.id.et_pwd_old)
    EditText etPwdOld;
    @BindView(R.id.et_pwd_new)
    EditText etPwdNew;
    @BindView(R.id.et_pwd_new_confirm)
    EditText etPwdNewConfirm;


    private String pwdOld;
    private String pwdNew;
    private String pwdNewConfirm;


    @Override
    protected int layoutId() {
        return R.layout.activity_change_pwd;
    }

    @Override
    protected void initView() {
        ToolBarManager.with(this, WMContent)
                .setBackgroundColor(R.color.toolbar_bg_grey)
                .setNavigationIcon(R.drawable.ic_nav_arrow_back)
                .setTitle("修改登录密码")
                .setTitleColor(R.color.black_normal);
    }

    private void getValue() {
        pwdOld = MyEditTextUtils.getContent(etPwdOld);
        pwdNew = MyEditTextUtils.getContent(etPwdNew);
        pwdNewConfirm = MyEditTextUtils.getContent(etPwdNewConfirm);
    }

    private boolean checkValue() {
        getValue();
        if (TextUtils.isEmpty(pwdOld)) {
            WMToast.normal("请输入旧密码");
            return false;
        }
        if (TextUtils.isEmpty(pwdNew)) {
            WMToast.normal("请输入新密码");
            return false;
        }
        if (TextUtils.isEmpty(pwdNewConfirm)) {
            WMToast.normal("请确定新密码");
            return false;
        }
        if (!TextUtils.equals(pwdNew, pwdNewConfirm)) {
            WMToast.normal("请确定两次输入的密码是一致的");
            return false;
        }
        return true;
    }

    @OnClick(R.id.btn_submit)
    public void onClickSubmit() {
        if (!checkValue())
            return;
        RetrofitHome.getInstance()
                .getInternalApi()
                .changePassword(AppData.getToken(), pwdOld, pwdNew)
                .compose(RxSchedulers.<BaseResponseB>toMain())
                .subscribe(new OnRequestListener<BaseResponseB>(this) {
                    @Override
                    public void onNext(BaseResponseB value) {
                        super.onNext(value);
                        if (value.getCode() == Const.REQUEST_SUCCESS) {
                            WMToast.normal("修改登录密码成功");
                            onBackPressed();
                        }
                    }
                });
    }
}
